package com.doophan.dac.bridge.controller;

import com.alibaba.fastjson.JSONObject;
import com.doophan.dac.bridge.socket.SocketServerRunner;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

@RestController
@RequestMapping("/bridge")
public class BridgeController {

    @RequestMapping("/proxy")
    public String proxy() {
        return null;
    }

    @PostMapping("/invoke")
    public Object test(@RequestBody JSONObject json) throws IOException {
        json.put("cmd", "IVK");
        String reply;
        // 获取空闲的socket
        Socket socket = SocketServerRunner.getFreeSocket();
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        dos.writeUTF(json.toJSONString());
        dos.flush();
        reply = dis.readUTF();
        // 释放socket
        SocketServerRunner.releaseSocket(socket);
        return reply;
    }

    @GetMapping("/download")
    public void download(@RequestParam String path, HttpServletResponse response) throws IOException {
        JSONObject json = new JSONObject();
        json.put("path", path);
        json.put("cmd", "DLD");
        Socket socket = SocketServerRunner.getFreeSocket();
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        dos.writeUTF(json.toJSONString());
        dos.flush();
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        System.out.println("======== 开始传输文件 ========");
        byte[] bytes = new byte[1024];
        String filename = getFilename(path);
        response.addHeader("Content-Disposition", "attachment;filename=" + filename);
        OutputStream os = response.getOutputStream();
        int length;
        while((length = dis.read(bytes, 0, bytes.length)) != -1) {
            os.write(bytes, 0, length);
            os.flush();
        }
        System.out.println("======== 文件传输成功 ========");
    }

    private static String getFilename(String path) {
        int pos = path.lastIndexOf("\\");
        return path.substring(pos, path.length());
    }

    @GetMapping("dir")
    public String dir(@RequestParam String path) throws IOException {
        JSONObject json = new JSONObject();
        json.put("path", path);
        json.put("cmd", "DLD");
        String reply;
        // 获取空闲的socket
        Socket socket = SocketServerRunner.getFreeSocket();
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        dos.writeUTF(json.toJSONString());
        dos.flush();
        reply = dis.readUTF();
        // 释放socket
        SocketServerRunner.releaseSocket(socket);
        return reply;
    }

    @PostMapping("exec")
    public String exec(@RequestBody JSONObject json) throws IOException {
        json.put("cmd", "EXE");
        String reply;
        // 获取空闲的socket
        Socket socket = SocketServerRunner.getFreeSocket();
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        dos.writeUTF(json.toJSONString());
        dos.flush();
        reply = dis.readUTF();
        // 释放socket
        SocketServerRunner.releaseSocket(socket);
        return reply;
    }
}
